
import wx
from wx import stc
from CodeCtrl import LuaSTC

debug_line_arrow = "#2D70A7"
debug_line_back = "#C6DBAE"
bp_line_arrow = "red"

def SetConfigDict(d):
	global debug_line_arrow, debug_line_back, bp_line_arrow
	if d.has_key('debug_line_arrow'):
		debug_line_arrow = d['debug_line_arrow']
	if d.has_key('debug_line_back'):
		debug_line_back = d['debug_line_back']
	if d.has_key('bp_line_arrow'):
		bp_line_arrow = d['bp_line_arrow']


class CodeEditor(LuaSTC):
	def __init__(self, parent):
		LuaSTC.__init__(self, parent, -1, style=wx.BORDER_NONE)
		self.SetUpEditor()

		from CodeCtrl import faces
		self.faces = faces

	# Some methods to make it compatible with how the wxTextCtrl is used
	def SetValue(self, value):
		if wx.USE_UNICODE:
			# TODO: Unicode support
			value = value.decode('iso8859_1')
		self.SetText(value)
		self.EmptyUndoBuffer()
		self.SetSavePoint()

	def IsModified(self):
		return self.GetModify()

	def Clear(self):
		self.ClearAll()

	def SetUpEditor(self):

		self.SetMarginType(1, wx.stc.STC_MARGIN_NUMBER)
		self.SetMarginSensitive(1, True)
		self.SetMarginWidth(1, 40)

		# For Debug
		self.DEBUG_MARK = 11
		self.DEBUG_MARK2 = 12
		self.DEBUG_COLOR1 = debug_line_arrow
		self.DEBUG_COLOR2 = debug_line_back
		self.MarkerDefine(self.DEBUG_MARK, stc.STC_MARK_SHORTARROW, self.DEBUG_COLOR1, self.DEBUG_COLOR1)
		self.MarkerDefine(self.DEBUG_MARK2, stc.STC_MARK_BACKGROUND, self.DEBUG_COLOR2, self.DEBUG_COLOR2)

		# For Break Point
		self.BP_MARK = 13
		self.BP_COLOR = bp_line_arrow
		self.MarkerDefine(self.BP_MARK, stc.STC_MARK_CIRCLE, self.BP_COLOR, self.BP_COLOR)


		self.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarker, self)
		self.clickLineNumHandler = None

	def RegisterModifiedEvent(self, eventHandler):
		self.Bind(wx.stc.EVT_STC_CHANGE, eventHandler)

	def RegisterLineNumberClickedEvent(self, eventHandler):
		self.clickLineNumHandler = eventHandler

	def OnMarker(self, event):
		if self.clickLineNumHandler:
			linNum = self.LineFromPosition(event.GetPosition())
			self.clickLineNumHandler(linNum)


##################################################

def runTest(frame, nb, log):
	ed = p = CodeEditor(nb)
	ed.SetText(open('../src/toydebugger.lua').read())
	return p

if __name__ == '__main__':
	import sys,os
	import wxrun
	wxrun.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

# vim: foldmethod=marker:
